#include "qae_qsettingsdialog.h"
#include "ui_settingsdialog.h"
#include "qae_global.h"
#include <QtGui/QFont>


namespace QAnsiEditor {


QSettingsDialog::QSettingsDialog(QWidget *parent) : QDialog(parent)
{
    setupUi(this);
    this->init();
    this->connect(bbOkCancel, SIGNAL(accepted()), this, SLOT(accept()));
    this->connect(bbOkCancel, SIGNAL(rejected()), this, SLOT(reject()));
}

bool QSettingsDialog::init()
{
    QGlobal *pGlobal = QGlobal::instance();
    // ESC
    cbEsc->addItem("ESC", "\x1b");
    cbEsc->addItem("ESC+ESC", "\x1b\x1b");
    cbEsc->setCurrentIndex(cbEsc->findData(pGlobal->escapeString()));
    // input encoding
    cbInput->addItem("GBK", QGlobal::GBK);
    cbInput->addItem("Big5", QGlobal::Big5);
    cbInput->addItem("UTF-8", QGlobal::UTF8);
    cbInput->setCurrentIndex(pGlobal->inputEncoding());
    // output encoding
    cbOutput->addItem("GBK", QGlobal::GBK);
    cbOutput->addItem("Big5", QGlobal::Big5);
    cbOutput->addItem("UTF-8", QGlobal::UTF8);
    cbOutput->setCurrentIndex(pGlobal->outputEncoding());
    // fonts
    cbAscii->setCurrentFont(QFont(pGlobal->asciiFont()));
    cbNonAscii->setCurrentFont(QFont(pGlobal->nonAsciiFont()));

    return true;
}

void QSettingsDialog::accept()
{
    QGlobal *pGlobal = QGlobal::instance();
    pGlobal->setEscapeString(cbEsc->itemData(cbEsc->currentIndex()).toString());
    pGlobal->setInputEncoding((QGlobal::Encoding)cbInput->itemData(cbInput->currentIndex()).toInt());
    pGlobal->setOutputEncoding((QGlobal::Encoding)cbOutput->itemData(cbOutput->currentIndex()).toInt());
    pGlobal->setAsciiFont(cbAscii->currentText());
    pGlobal->setNonAsciiFont(cbNonAscii->currentText());
    pGlobal->saveSettings();
    // TODO: maybe should refresh to repaint here.
    QDialog::accept();
}


}  // namespace QAnsiEditor
